import { getArticleClassify } from '@/services';
import { IArticleClassifyType} from '@/types';
import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';

export interface ClassifyModelState {
    articleClassify:IArticleClassifyType [],
}

export interface TagModelType {
  namespace: 'articleClassify';
  state: ClassifyModelState;
  effects: {
    query: Effect;
    getArticleClassify: Effect;
  };
  reducers: {
    save: Reducer<ClassifyModelState>;
  };
}

const ClassifyModelState: TagModelType = {
  // 数据模型，用来隔离数据防止相互影响
  namespace: 'articleClassify',

  // 数据模型状态，相当于vuex的state
  state: {
    articleClassify: [],
  },

  // 异步操作，相当于vuex的action
  effects: {
    // 先简单记忆，*相当于async，yield相当于await
    *query({ payload }, { call, put }) {},
    *getArticleClassify({payload}, {call, put}){
      let result = yield getArticleClassify();
      if (result.data){
        yield put({
          type: 'save',
          payload: {
            articleClassify: result.data,
          }
        })
      }
    }
  },

  // 同步操作，相当于vuex的mutation
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },

  }
};

export default ClassifyModelState;
